﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;


// Scaffold-DbContext "Data Source=.;Initial Catalog=SAMPLEDB;User ID=sa;Password=root123" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDbContext -Force
namespace mvc6.Models
{
    public partial class MyDbContext : DbContext
    {
        public MyDbContext()
        {
        }

        public MyDbContext(DbContextOptions<DbContext> options)
            : base(options)
        {
        }

        public virtual DbSet<Company> Companys { get; set; } = null!;
        public virtual DbSet<Customer> Customers { get; set; } = null!;
        public virtual DbSet<Employee> Employees { get; set; } = null!;
        public virtual DbSet<HourlyEmployee> HourlyEmployees { get; set; } = null!;
        public virtual DbSet<Image> Images { get; set; } = null!;
        public virtual DbSet<Order> Orders { get; set; } = null!;
        public virtual DbSet<SalariedEmployee> SalariedEmployees { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=SAMPLEDB;User ID=sa;Password=root123");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Company>(entity =>
            {
                entity.ToTable("COMPANYS");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.Name)
                    .HasMaxLength(50)
                    .HasColumnName("NAME");
            });

            modelBuilder.Entity<Customer>(entity =>
            {
                entity.ToTable("CUSTOMERS");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.Age).HasColumnName("AGE");

                entity.Property(e => e.ComCity)
                    .HasMaxLength(50)
                    .HasColumnName("COM_CITY");

                entity.Property(e => e.ComProvince)
                    .HasMaxLength(50)
                    .HasColumnName("COM_PROVINCE");

                entity.Property(e => e.ComStreet)
                    .HasMaxLength(50)
                    .HasColumnName("COM_STREET");

                entity.Property(e => e.ComZipcode)
                    .HasMaxLength(50)
                    .HasColumnName("COM_ZIPCODE");

                entity.Property(e => e.HomeCity)
                    .HasMaxLength(50)
                    .HasColumnName("HOME_CITY");

                entity.Property(e => e.HomeProvince)
                    .HasMaxLength(50)
                    .HasColumnName("HOME_PROVINCE");

                entity.Property(e => e.HomeStreet)
                    .HasMaxLength(50)
                    .HasColumnName("HOME_STREET");

                entity.Property(e => e.HomeZipcode)
                    .HasMaxLength(50)
                    .HasColumnName("HOME_ZIPCODE");

                entity.Property(e => e.Name)
                    .HasMaxLength(15)
                    .IsUnicode(false)
                    .HasColumnName("NAME");
            });

            modelBuilder.Entity<Employee>(entity =>
            {
                entity.ToTable("EMPLOYEES");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.CompanyId).HasColumnName("COMPANY_ID");

                entity.Property(e => e.EmployeeType)
                    .HasMaxLength(50)
                    .HasColumnName("EMPLOYEE_TYPE");

                entity.Property(e => e.Name)
                    .HasMaxLength(50)
                    .HasColumnName("NAME");

                entity.Property(e => e.Rate).HasColumnName("RATE");

                entity.Property(e => e.Salary).HasColumnName("SALARY");
            });

            modelBuilder.Entity<HourlyEmployee>(entity =>
            {
                entity.ToTable("HOURLY_EMPLOYEES");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.CompanyId).HasColumnName("COMPANY_ID");

                entity.Property(e => e.Name)
                    .HasMaxLength(50)
                    .HasColumnName("NAME");

                entity.Property(e => e.Rate).HasColumnName("RATE");

                entity.HasOne(d => d.Company)
                    .WithMany(p => p.HourlyEmployees)
                    .HasForeignKey(d => d.CompanyId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_HOURLY_EMPLOYEES_COMPANYS");
            });

            modelBuilder.Entity<Image>(entity =>
            {
                entity.HasKey(e => new { e.CustomerId, e.FileName });

                entity.ToTable("Image");

                entity.Property(e => e.CustomerId).HasColumnName("Customer_ID");

                entity.Property(e => e.FileName).HasMaxLength(50);

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.Images)
                    .HasForeignKey(d => d.CustomerId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_Image_CUSTOMERS");
            });

            modelBuilder.Entity<Order>(entity =>
            {
                entity.ToTable("ORDERS");

                entity.Property(e => e.Id)
                    .ValueGeneratedNever()
                    .HasColumnName("ID");

                entity.Property(e => e.CustomerId).HasColumnName("CUSTOMER_ID");

                entity.Property(e => e.OrderNumber)
                    .HasMaxLength(15)
                    .IsUnicode(false)
                    .HasColumnName("ORDER_NUMBER");

                entity.Property(e => e.Price)
                    .HasColumnType("decimal(18, 0)")
                    .HasColumnName("PRICE");

                entity.HasOne(d => d.Customer)
                    .WithMany(p => p.Orders)
                    .HasForeignKey(d => d.CustomerId)
                    .HasConstraintName("FK_ORDERS_CUSTOMERS");
            });

            modelBuilder.Entity<SalariedEmployee>(entity =>
            {
                entity.ToTable("SALARIED_EMPLOYEES");

                entity.Property(e => e.Id).HasColumnName("ID");

                entity.Property(e => e.CompanyId).HasColumnName("COMPANY_ID");

                entity.Property(e => e.Name)
                    .HasMaxLength(50)
                    .HasColumnName("NAME");

                entity.Property(e => e.Salary).HasColumnName("SALARY");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
